using Microsoft.SmallBasic.Library;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace SmallBasicFun.Tests.HomeworkTests
{
    [TestClass]
    public class HomeworkForSimpleSquareTest
    {
        private static readonly Primitive ___ = "Fill in the blank ___";
        private static readonly Primitive ____ = 1;

        private static readonly Primitive topSpeed = 10;
        private static readonly Primitive equals = "=";
        private static int answerUntil;
        private static int answersTried;

        [TestMethod]
        public void TestQuiz()
        {
            SetQuestionsToAnswer(19);
            Homework.LastQuestionAnsweredCorrectly = ()=>0;
            Homework.StartSimpleSquareHomeworkAt += numbersdonotneedquotes;
            ApprovalHelper.ApproveProgramWindow();
        }

        private void SetQuestionsToAnswer(int until)
        {
            answerUntil = until;
            answersTried = 0;
        }

        [TestMethod]
        public void TestColor()
        {
            SetQuestionsToAnswer(1);
            changingthepencolortogreen();
        }
        [TestMethod]
        public void TestLastQuestion()
        {
            Homework.ReadLastQuestionFromRegistry();
           Homework.WriteLastQuestionToRegistry(-13);
            Assert.AreEqual(-13, Homework.ReadLastQuestionFromRegistry());
        }

        private static Primitive getAnswer(Primitive ifTrying)
        {
            Primitive ans = answerUntil > answersTried ? ifTrying : ___;
            answersTried++;
            return ans;
        }

        private static void numbersdonotneedquotes()
        {
            Homework.CheckAnswer(getAnswer(42), equals, 42);
        }

        private static void defaultwidthforthetortoise()
        {
            Homework.CheckAnswer(getAnswer(2), equals, Tortoise.GetPenWidth());
        }

        private static void stringsneedquotes()
        {
            Homework.CheckAnswer(getAnswer("Green"), equals, "Green");
        }
        
        private static void stringscanincludespaces()
        {
            Homework.CheckAnswer(getAnswer("This is a string"), equals, "This is a string");
        }

        private static void defaultcolorforthetortoise()
        {
            Homework.CheckAnswer(getAnswer("Black"), equals, Tortoise.GetPenColor());
        }

        private static void changingthepenwidthto5()
        {
            Tortoise.SetPenWidth(getAnswer(5));
            Homework.CheckAnswer(5, equals, Tortoise.GetPenWidth());
        }

        private static void changingthepencolortogreen()
        {
            Tortoise.SetPenColor(getAnswer("green"));
            Homework.CheckAnswer( Tortoise.GetPenColor(), equals, "Green");
        }

        private static void movingthetortoise100pixels()
        {
            Primitive start = Tortoise.GetY();
            Tortoise.Move(getAnswer(100));
            Homework.CheckAnswer(start - 100, equals, Tortoise.GetY());
        }

        private static void thetortoiseturns21()
        {
            Tortoise.Turn(getAnswer(21));
            Homework.CheckAnswer(21, equals, Tortoise.GetAngle());
        }

        private static void thetortoiseturns15twice()
        {
            Primitive ans = getAnswer(15);
            Tortoise.Turn(ans);
            Tortoise.Turn(ans);
            Homework.CheckAnswer(30, equals, Tortoise.GetAngle());
        }

        private static void howfastcanthetortoisego()
        {
            Tortoise.SetSpeed(getAnswer(10));
            Homework.CheckAnswer(topSpeed, equals, Tortoise.GetSpeed());
//Hint: Click SetSpeed then read the documentation on the left ----->
        }

        private static void assigningvariables()
        {
            Primitive numberOfToes = 10;
            Homework.CheckAnswer(getAnswer(10), equals, numberOfToes);
        }

        private static void combiningnumbers()
        {
            Primitive age = 3 + 4;
            Homework.CheckAnswer(getAnswer(7), equals, age);
        }

        private static void combiningtext()
        {
            Primitive name = "Peter" + " " + "Pan";
            Homework.CheckAnswer(getAnswer("Peter Pan"), equals, name);
        }

        private static void combiningtextandnumbers()
        {
            Primitive name = "Henry The " + 8;
            Homework.CheckAnswer(getAnswer("Henry The 8"), equals, name);
        }

        private static void combiningtextinaloop()
        {
            Primitive sound = "A";
            for (int i = 1; i <= 3; i++)
            {
                sound = sound + "H";
            }
            Homework.CheckAnswer(getAnswer("AHHH"), equals, sound);
        }

        private static void forloopsendattheend()
        {
            Primitive numbers = "#";
            for (int i = 1; i <= getAnswer(5); i++)
            {
                numbers = numbers + i;
            }
            Homework.CheckAnswer(numbers, equals, "#12345");
        }

        private static void forloopscanstartanywhere()
        {
            Primitive answer = "Because ";
            for (int i = getAnswer(7); i <= 9; i++)
            {
                answer = answer + i;
            }
            //Question: Why is 7 the most feared number ?
            Homework.CheckAnswer(answer, equals, "Because 789");
        }

        private static void forloopscanskip()
        {
            Primitive numbers = "#";
            for (int i = 1; i <= 6; i+= getAnswer(2))
            {
                numbers = numbers + i;
            }
            Homework.CheckAnswer(numbers, equals, "#135");
        }

        private static void forloopscangobackwards()
        {
            Primitive numbers = "Countdown: ";
            for (int i = 5; i >= 1; i += getAnswer(-1))
            {
                numbers = numbers + i;
            }
            Homework.CheckAnswer(numbers, equals, "Countdown: 54321");
        }
    }
}